package com.hxkj.cst.chengshuitong.ui.fragment

import android.app.AlertDialog
import android.view.View
import android.widget.TextView
import com.hxkj.cst.chengshuitong.R
import com.hxkj.cst.chengshuitong.RatePaymentEnum
import com.hxkj.cst.chengshuitong.XzqhBean
import com.hxkj.cst.chengshuitong.base.RatePaymentFragment
import com.hxkj.cst.chengshuitong.control.FirstRatePaymentControl
import com.hxkj.cst.chengshuitong.presenter.FirstRatePaymentPresenter
import com.hxkj.cst.chengshuitong.ui.adapter.WheelViewAdapter
import com.hxkj.cst.chengshuitong.widget.WheelView
import kotlinx.android.synthetic.main.fragment_first_payment.view.*
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

/**
 * 作者：LuoYang
 * 时间：2017/5/31:15:39
 * 邮箱：18981443691@163.com
 * 说明：
 */
class RatePaymentFirstFragment : RatePaymentFragment(), FirstRatePaymentControl.View {
    var mWvProvince: WheelView? = null
    var mWvCity: WheelView? = null
    var mTvSelect: TextView? = null
    var mDialog: AlertDialog? = null

    var mProvinceList: List<XzqhBean>? = null

    val WV_PROVINCE = 0
    val WV_CITY = 1

    override fun provideContentView(): Int = R.layout.fragment_first_payment

    override fun initLayout(view: View) {
        mWvProvince = view.ffirst_wv_province
        mWvCity = view.ffirst_wv_city
        mTvSelect = view.ffirst_tv_select

        view.ffirst_btn_next.setOnClickListener { showSelectCarType() }

        FirstRatePaymentPresenter(this, activity).loadAreaDataSource()
    }

    fun showSelectCarType() {
        if (mDialog == null)
            mDialog = AlertDialog.Builder(activity)
                    .setTitle("提示")
                    .setIcon(R.mipmap.ic_launcher)
                    .setItems(arrayOf("国产车", "进口车"), { dialog, _ ->
                        dialog.dismiss()
                        mRatePaymentObserver?.update(RatePaymentEnum.CAPTURE_SBB)
                    })
                    .setNegativeButton("取消", { dialog, _ ->
                        dialog.dismiss()
                    })
                    .create()
        mDialog?.show()
    }

    override fun setUpWheelView(arealList: ArrayList<XzqhBean>, default: Int) {
        mProvinceList = arealList

        val provinceAdapter = WheelViewAdapter(arealList)
        mWvProvince?.setWheelViewAdapter(provinceAdapter)
        mWvProvince?.setDefault(default)
        mWvProvince?.setOnSelectListener(mOnSelectListener, WV_PROVINCE)

        val cityList: ArrayList<XzqhBean> = arealList[default].XSXZQH
        if (cityList.isNotEmpty()) {
            loadCityWheelView(cityList)
        }
        showSelectArea()
    }

    fun loadCityWheelView(cityList: ArrayList<XzqhBean>) {
        if (cityList.isEmpty()) {
            cityList.add(XzqhBean(0, "", ArrayList()))
        }
        val cityAdapter = WheelViewAdapter(cityList)
        mWvCity?.setWheelViewAdapter(cityAdapter)
        mWvCity?.setDefault(0)
        mWvCity?.setOnSelectListener(mOnSelectListener, WV_CITY)
    }

    fun showSelectArea() {
        val provinceName = mWvProvince?.selectedText
        val cityName = mWvCity?.selectedText
        mTvSelect?.text = "$provinceName $cityName"
    }

    val mOnSelectListener: WheelView.OnSelectListener =
            WheelView.OnSelectListener { id, itemText, tag ->
                Observable.just(Pair<Int, String>(id, itemText))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe({
                            if (tag == WV_PROVINCE) {
                                val cityList = mProvinceList?.get(id)?.XSXZQH
                                cityList?.let { loadCityWheelView(it) }
                            }
                            showSelectArea()
                        }, Throwable::printStackTrace)
            }
}